import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
import tabBar from '@/utils/tabbar.js'
const store = new Vuex.Store({

	state() {
		return {
			token: uni.getStorageSync('token'),
			tabBarList: uni.getStorageSync('tabBarList'),
			roleId: uni.getStorageSync('roleId'),
		}
	},
	mutations: {
		// 设置token
		setToken(state, data) {

			state.token = data;
			uni.setStorageSync('token', data)
		},
		// 设置用户角色ID
		setRoleId(state, data) {
			state.roleId = data;
			uni.setStorageSync('roleId', data)
			state.tabBarList = tabBar[data];
			uni.setStorageSync('tabBarList', tabBar[data])
		},
		// 检查token是否存在
		checkToken(state) {
			let token = uni.getStorageSync('token')
			if (token && state.token) {
				return
			} else {
				// console.log(token)
				// console.log(state)
				uni.navigateTo({
					url: "/pages/index/index"
				})

			}
		},
		// 检查权限
		checkTeacherToken(state) {
			let token = uni.getStorageSync('token')
			if (token && state.token && state.roleId) {
				return
			} else {
				state = {
					token: '',
					tabBarList: [],
					roleId: 0, //0 普通员工，1管理员
				};
				uni.removeStorageSync('tabBarList');
				uni.removeStorageSync('roleId');
				uni.removeStorageSync('token');
				uni.navigateTo({
					url: "/pages/index/index"
				})
			}
		},
		// 清除信息
		clearAll(state) {
			state = {
				token: '',
				tabBarList: [],
				roleId: 0, //0 普通员工，1管理员
			};
			uni.removeStorageSync('tabBarList');
			uni.removeStorageSync('roleId');
			uni.removeStorageSync('token');
		}
	},
})
export default store
